﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Diagnostics;


namespace SpaceArcade
{
    class Sprite
    {
        public Bitmap bm;        
        
        protected int x, y;

        public int X
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }

        public int Y
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
            }
        }

        public Sprite(string path, int x, int y)
        {
            this.bm = new Bitmap(Image.FromFile(GamePath.TexturesPath + path));
            this.x = x;
            this.y = y;            
        }

        

        public Sprite() { }

        public void ChangeImage(string newPath)
        {
            this.bm = new Bitmap(Image.FromFile(GamePath.TexturesPath + newPath));            
        }

        public virtual void Draw(Bitmap gameBM)
        {
            Graphics.FromImage(gameBM).DrawImage(this.bm, x, y);
        }   
        
    }

    class AnimatedSprite : Sprite
    {
        new public List<Bitmap> bm;
        protected Bitmap _bm;
        protected Stopwatch st;
        protected int CurrFrame;
        public int _frameWidth, _frameHeight;
        protected int firstFrame, lastFrame;

        public int FrameWidth
        {
            get
            {
                return _frameWidth;
            }
            private set
            {
                _frameWidth = value;
            }
        }

        public int FrameHeight
        {
            get
            {
                return _frameHeight;
            }
            private set
            {
                _frameHeight = value;
            }
        }

        public AnimatedSprite(string path, int x, int y, int framesCount)
            : base(path,x, y)
        {
            this.x = x;
            this.y = y;
            

            _bm = new Bitmap(Image.FromFile(GamePath.TexturesPath + path));
            bm = new List<Bitmap>();
            
            int currX = 0;
            _frameHeight = _bm.Height;
            _frameWidth = _bm.Width / framesCount;
            while (currX < _bm.Width - _frameWidth)
            {
                Rectangle rect = new Rectangle(currX, 0, _frameWidth, _frameHeight);                
                bm.Add(_bm.Clone(rect, _bm.PixelFormat));
                currX += _frameWidth;
            }
            st = new Stopwatch();
            st.Start();
            
        }

        public AnimatedSprite() { }

        public void SetFramesRange(int FirstFrame, int LastFrame)
        {
            this.firstFrame = FirstFrame;
            this.lastFrame = LastFrame;
            CurrFrame = firstFrame;
        }

        public void ChangeTexture(string newPath, int framesCount)
        {
            _bm = new Bitmap(Image.FromFile(GamePath.TexturesPath + newPath));
            bm = new List<Bitmap>();            

            int currX = 0;
            _frameHeight = _bm.Height;
            _frameWidth = _bm.Width / framesCount;
            while (currX < _bm.Width - _frameWidth)
            {
                Rectangle rect = new Rectangle(currX, 0, _frameWidth, _frameHeight);
                bm.Add(_bm.Clone(rect, _bm.PixelFormat));
                currX += _frameWidth;
            }
            SetFramesRange(0, framesCount - 1);
        }       

        public override void Draw(Bitmap gameBM)
        {
           
            if (bm.Count > 0)
            {
                Graphics.FromImage(gameBM).DrawImage(bm[CurrFrame], this.x, this.y);
                var dt = st.ElapsedMilliseconds;
                if (dt > 50)
                {
                    CurrFrame++;
                    dt = 0;
                    st.Restart();
                }
                if (CurrFrame >= lastFrame)
                    CurrFrame = firstFrame;
            }           
        }

        public virtual bool Intersects(AnimatedSprite object2)
        {          
            if (object2 != null)
            {
                if (this.X + _frameWidth - 45 < object2.X || object2.X + object2._frameWidth - 45 < this.X || this.Y + _frameHeight < object2.Y || object2.Y + object2._frameHeight - 27 < this.Y)
                    return false;
                else
                    return true;
            }
            else
                return false;          
        }       
    }    
}
